package com.philips.work.service.impl;

import java.util.Date;
import java.util.List;

import com.philips.common.core.exception.ServiceException;
import com.philips.common.core.utils.StringUtils;
import com.philips.work.domain.YwAccount;
import com.philips.work.domain.YwDict;
import com.philips.work.mapper.YwAccountMapper;
import com.philips.work.mapper.YwDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.philips.work.mapper.YwFlowMapper;
import com.philips.work.domain.YwFlow;
import com.philips.work.service.IYwFlowService;

/**
 * 总流水Service业务层处理
 * 
 * @author philips
 * @date 2025-05-20
 */
@Service
public class YwFlowServiceImpl implements IYwFlowService 
{
    @Autowired
    private YwFlowMapper ywFlowMapper;
    @Autowired
    private YwAccountMapper ywAccountMapper;
    @Autowired
    private YwDictMapper ywDictMapper;

    /**
     * 查询总流水
     * 
     * @param ids 总流水主键
     * @return 总流水
     */
    @Override
    public YwFlow selectYwFlowByIds(String ids)
    {
        return ywFlowMapper.selectYwFlowByIds(ids);
    }

    /**
     * 查询总流水列表
     * 
     * @param ywFlow 总流水
     * @return 总流水
     */
    @Override
    public List<YwFlow> selectYwFlowList(YwFlow ywFlow)
    {
        return ywFlowMapper.selectYwFlowList(ywFlow);
    }

    /**
     * 新增总流水
     * 
     * @param ywFlow 总流水
     * @return 结果
     */
    @Override
    public int insertYwFlow(YwFlow ywFlow)
    {
        judgeImport(ywFlow);
        return ywFlowMapper.insertYwFlow(ywFlow);
    }

    /**
     * 修改总流水
     * 
     * @param ywFlow 总流水
     * @return 结果
     */
    @Override
    public int updateYwFlow(YwFlow ywFlow)
    {
        judgeImport(ywFlow);
        ywFlow.setUpTime(new Date());
        return ywFlowMapper.updateYwFlow(ywFlow);
    }

    /**
     * 批量删除总流水
     * 
     * @param idss 需要删除的总流水主键
     * @return 结果
     */
    @Override
    public int deleteYwFlowByIdss(String[] idss)
    {
        return ywFlowMapper.deleteYwFlowByIdss(idss);
    }

    /**
     * 删除总流水信息
     * 
     * @param ids 总流水主键
     * @return 结果
     */
    @Override
    public int deleteYwFlowByIds(String ids)
    {
        return ywFlowMapper.deleteYwFlowByIds(ids);
    }

    /**
     * 导入数据
     *
     * @param flowList 数据列表 YwFlow ywFlow
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importFlow(List<YwFlow> flowList, boolean updateSupport)
    {
        if (StringUtils.isNull(flowList) || flowList.isEmpty())
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (YwFlow po : flowList)
        {
            try
            {
                /*
                目前只要写入，不管更新的问题
                 */
                // 3中類型和4個是否有問題的狀態維護
                judgeImport(po);
                ywFlowMapper.insertYwFlow(po);
                successNum++;
                successMsg.append("<br/>").append(successNum).append(" 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum  + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 判断导入内容是否和标准库一致，同时赋值整表是否有问题的判断结果
     * @param ywFlow
     */
    public void judgeImport(YwFlow ywFlow) {
        // 1. 處理賬戶信息
        Long accountId = ywFlow.getAccountId();
        String accountName = ywFlow.getAccountName();
        // 1.1 導入情況，ID為空，name有值
        if(accountId==null){
            if(StringUtils.isNotEmpty(accountName)){
                YwAccount ywAccount = new YwAccount();
                ywAccount.setAccountName(accountName);
                List<YwAccount> ywAccounts = ywAccountMapper.selectYwAccountList(ywAccount);
                // 1.1.1 通過賬戶名查到了賬戶信息，標準，棒！
                if(ywAccounts!=null && !ywAccounts.isEmpty()){
                    ywFlow.setAccountId(Long.parseLong(ywAccounts.get(0).getIds()));
                    ywFlow.setIsAccount("1");
                }else{
                    // 1.1.2 通過賬戶名沒有查到賬戶信息，記錄數據有問題！
                    ywFlow.setIsAccount("0");
                }
            }else{
                // 1.1.3 導入數據進來的，但是也沒有提供賬戶信息，記錄數據有問題！
                ywFlow.setIsAccount("0");
            }
        }
        // 1.2 通過系統寫入或更新操作
        if(accountId!=null){
            YwAccount ywAccount = ywAccountMapper.selectYwAccountByIds(accountId.toString());
            accountName = ywAccount.getAccountName();
            ywFlow.setAccountName(accountName);
            // 系統寫入一定是正確的，就算是錯的，上一行代碼也改對了
            ywFlow.setIsAccount("1");
        }

        // 2. 處理備注類型
        Long remarkId = ywFlow.getRemarkId();
        String remark = ywFlow.getRemark();
        // 2.1 導入情況，ID為空，name有值
        if(remarkId==null){
            if(StringUtils.isNotEmpty(remark)){
                YwDict ywDict = new YwDict();
                ywDict.setDictType("flowRemark");
                ywDict.setDictName(remark);
                List<YwDict> ywDicts = ywDictMapper.selectYwDictList(ywDict);
                // 2.1.1 通過備注類型名查到了備注類型信息，標準，棒！
                if(ywDicts!=null && !ywDicts.isEmpty()){
                    ywFlow.setRemarkId(Long.parseLong(ywDicts.get(0).getIds()));
                    ywFlow.setIsRemark("1");
                }else{
                    // 2.1.2 通過備注類型名沒有查到備注類型信息，記錄數據有問題！
                    ywFlow.setIsRemark("0");
                }
            }else{
                // 2.1.3 導入數據進來的，但是也沒有提供備注類型信息，記錄數據有問題！
                ywFlow.setIsRemark("0");
            }
        }
        // 2.2 通過系統寫入或更新操作
        if(remarkId!=null){
            YwDict ywDict = ywDictMapper.selectYwDictByIds(remarkId.toString());
            remark = ywDict.getDictName();
            ywFlow.setRemark(remark);
            // 系統寫入一定是正確的，就算是錯的，上一行代碼也改對了
            ywFlow.setIsRemark("1");
        }

        // 3. 判斷整條數據整體是否有問題
        if("1".equals(ywFlow.getIsAccount()) && "1".equals(ywFlow.getIsRemark())){
            ywFlow.setNoWrong("1");
        }else {
            ywFlow.setNoWrong("0");
        }
    }
}
